import {Component, Input, OnInit} from '@angular/core';
import {FormGroup} from '@angular/forms';
import {FormlyFormOptions} from '@ngx-formly/core';
import {ApesModalRef} from '@shared/apes/modal';
import {exists, extend, missing, missingOrEmpty} from '../../utils/utils';

@Component({
  selector: 'apes-oa',
  templateUrl: './apes-oa.component.html',
})
export class ApesOaComponent implements OnInit {
  form: FormGroup;
  modelData: any = {};

  wf_approval = {
    key: "wf_approval",
    type: "radio",
    templateOptions: {
      label: "操作",
      divCol: 24,
      lblCol: 4,
      inputCol: 20,
      required: true
    },
  };

  wf_comments = {
    key: "wf_comments",
    type: "textarea",
    templateOptions: {
      label: "意见",
      rows: 6,
      divCol: 24,
      lblCol: 4,
      inputCol: 20,
    },
  };

  wf_user = {
    key: "wf_user",
    type: "select",
    templateOptions: {
      init: true,
      multiple: 'false',
      label: "人员",
      api: "aut.user.findAllEnable",
      searchFormId: "rbac.user.findAllForOa",
      domain: '{"filters":[{"field":"name","operator":"LIKE","value":"\'{{value}}\'"}],"groups":[],"operator":"and"}',
      divCol: 24,
      lblCol: 4,
      inputCol: 20,
      required: true
    }
  };

  wf_carbonCopy = {
    key: "wf_carbonCopy",
    type: "select",
    templateOptions: {
      init: true,
      multiple: 'true',
      label: "抄送",
      api: "aut.user.findAllEnable",
      searchFormId: "rbac.user.findAllForOa",
      domain: '{"filters":[{"field":"name","operator":"LIKE","value":"\'{{value}}\'"}],"groups":[],"operator":"and"}',
      divCol: 24,
      lblCol: 4,
      inputCol: 20,
      required: false
    }
  };



  fields = [];

  options: FormlyFormOptions = {};

  constructor(private modal: ApesModalRef) {
  }

  ngOnInit() {
    this.form = new FormGroup({});
    this.modelData = {};
    let _exclusiveGateway = this.modal['apesComponentParams'].exclusiveGateway;
    if (!missingOrEmpty(_exclusiveGateway)) {
      let fieldName = '';
      _exclusiveGateway.forEach(data => {
        data.label = data.name;
        if (missing(fieldName) && exists(data.valueId)) {
          fieldName = 'wf_variable_' + data.valueId;
        }
      });
      if(exists(fieldName)){
        this.wf_approval.templateOptions['options'] = _exclusiveGateway;
        this.wf_approval.key = fieldName;
        this.fields.push(this.wf_approval);
      }
    }
    this.fields.push(this.wf_comments);
    this.fields.push(this.wf_carbonCopy); // 添加抄送按钮
    if(this.modal['apesComponentParams'].carbonCopy) this.wf_carbonCopy.templateOptions["defaultValue"] = this.modal['apesComponentParams'].carbonCopy;
    this.modelData['carbonCopy'] = this.modal['apesComponentParams'].carbonCopy;

    let id = this.modal['apesComponentParams'].id;
    if (id === 'action_wf_resolve' || id === 'action_wf_apostille' || id === 'action_wf_countersign') {
      if (id === 'action_wf_countersign') this.wf_user.templateOptions['multiple'] = 'true';
      this.fields.push(this.wf_user);
    }

    let extFields = this.modal['apesComponentParams'].extFields;
    if (!missingOrEmpty(extFields)) {
      extFields.forEach(field => {
        if (field.hasOwnProperty("fieldGroup") && field.fieldGroup.length === 0) {
          delete field.fieldGroup;
        }
        let fieldDef = extend(true, {}, field);
        fieldDef.templateOptions.divCol = 24;
        fieldDef.templateOptions.lblCol = 4;
        fieldDef.defaultValue = null;

        fieldDef.templateOptions.inputCol = 20;
        this.fields.push(fieldDef);
      });

      let context = this.modal['apesComponentParams'].context;
      if (exists(context)) {
        this.form.reset(context);
        for(let key in context){
          this.modelData[key] = context[key];
        }
      }
    }

  }

  destroyModal(): void {
    this.modal.destroy(this.modelData);
  }

}
